import axios from 'axios';
import { writeFileAsync } from '../fileOperate';
// import { readFile, writeFile } from 'fs/promises';
import { scrapePoemId } from './poem';
import { scrapePoemType } from './poem-type';
import { batchScrapeUrls } from './poem-ext';
import { batchScrapePoetUrls } from './poet-ext';
export async function fetchData(url: string): Promise<string | Buffer> {
  try {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
    const { data, status } = await axios.get(url);
    console.log(`请求 ${url} 成功，状态码: ${status}`);
    // eslint-disable-next-line @typescript-eslint/no-unsafe-return
    return data;
  } catch (error) {
    console.error(`Error fetching data from ${url}:`, error);
    return null;
  }
}

// 生成指定时间 ± 1 秒的随机延时
function getRandomDelay(delay: number): number {
  const min = delay - 1000;
  const max = delay + 1000;
  return Math.random() * (max - min) + min;
}

// 通用的分页爬取函数
export async function paginatedScrape(
  scrapePage: (url: string) => Promise<string[]>,
  baseUrl: string,
  totalPages: number,
  filePath: string,
  delay: number = 1000, // 单位：毫秒
): Promise<void> {
  let totalScraped = 0;
  for (let page = 1; page <= totalPages; page++) {
    const url = `${baseUrl}/${page}`; // 假设分页参数是 page
    try {
      const results = await scrapePage(url);
      totalScraped += results.length;
      // 逐行追加数据到文件
      for (const result of results) {
        await writeFileAsync(filePath, `"${result}",\n`);
      }
      console.log(`已爬取第 ${page} 页，当前总爬取数量: ${totalScraped}`);
      // 随机延时
      await new Promise((resolve) =>
        setTimeout(resolve, getRandomDelay(delay)),
      );
    } catch (error) {
      console.error(`Scraping page ${page} failed:`, error);
    }
  }
  // 修正 JSON 文件格式，添加方括号
  // await fixJsonFileFormat(filePath);
}
// 修正 JSON 文件格式
// async function fixJsonFileFormat(filePath: string): Promise<void> {
//   try {
//     let data = await readFile(filePath, 'utf8');
//     // 去除最后一个逗号
//     data = data.replace(/,\s*$/, '');
//     // 添加方括号
//     data = `[${data}]`;
//     await writeFile(filePath, data);
//   } catch (error) {
//     console.error('Fixing JSON file format failed:', error);
//   }
// }

export default {
  fetchData,
  paginatedScrape,
  scrapePoemId,
  scrapePoemType,
  batchScrapeUrls,
  batchScrapePoetUrls,
};
